package com.wuliupai.delivery.wuliupaidelivery.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Descriptions：历史记录数据库的Helper
 * <p>
 * Author：ChenME
 * Date：2017/1/4
 * Email：ibelieve1210@163.com
 */
public class DBHelper {
    private final String tableName = "city_history";
    private Context mContext;
    private CityHistoryDBOpenHelper openHelper;

    public DBHelper(Context context) {
        this.mContext = context;
        openHelper = new CityHistoryDBOpenHelper(mContext);
    }

    /**
     * 插入一条记录
     *
     * @param bean
     * @return
     */
    public long insertCityHistory(CityHistoryBean bean) {
        CityHistoryBean data = getHistoryDataByAreaCode(bean.getAreaCode());
        if (null != data) {
            return updateHistoryData(bean);
        }

        SQLiteDatabase db = openHelper.getWritableDatabase();
        long result = 0;
        ContentValues cv = new ContentValues();
        cv.put("areaCode", bean.getAreaCode());
        cv.put("area", bean.getArea());
        cv.put("cityCode", bean.getCityCode());
        cv.put("city", bean.getCity());
        cv.put("provinceCode", bean.getProvinceCode());
        cv.put("province", bean.getProvince());
        cv.put("time", System.currentTimeMillis());
        result = db.insert(tableName, "area", cv);
        db.close();
        return result;
    }

//    /**
//     * 获取所有的历史记录
//     *
//     * @return
//     */
//    public List<CityHistoryBean> getAllHistoryData() {
//        List<CityHistoryBean> resultList = new ArrayList<>();
//        CityHistoryBean bean = null;
//        SQLiteDatabase db = openHelper.getWritableDatabase();
//        Cursor cursor = db.rawQuery("select * from city_history order by time desc", null);
//        while (cursor.moveToNext()) {
//            bean = new CityHistoryBean();
//            bean.setAreaCode(cursor.getString(cursor.getColumnIndex("areaCode")));
//            bean.setArea(cursor.getString(cursor.getColumnIndex("area")));
//            bean.setCityCode(cursor.getString(cursor.getColumnIndex("cityCode")));
//            bean.setCity(cursor.getString(cursor.getColumnIndex("city")));
//            bean.setProvinceCode(cursor.getString(cursor.getColumnIndex("provinceCode")));
//            bean.setProvince(cursor.getString(cursor.getColumnIndex("province")));
//            resultList.add(bean);
//        }
//        db.close();
//        return resultList;
//    }

    /**
     * 获取前Top条历史记录
     *
     * @return
     */
    public List<CityHistoryBean> getTopHistoryData(int top) {
        List<CityHistoryBean> resultList = new ArrayList<>();
        CityHistoryBean bean = null;
        SQLiteDatabase db = openHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery(String.format("select * from city_history order by time desc limit 0,%s", top + ""), null);
        while (cursor.moveToNext()) {
            bean = new CityHistoryBean();
            bean.setAreaCode(cursor.getString(cursor.getColumnIndex("areaCode")));
            bean.setArea(cursor.getString(cursor.getColumnIndex("area")));
            bean.setCityCode(cursor.getString(cursor.getColumnIndex("cityCode")));
            bean.setCity(cursor.getString(cursor.getColumnIndex("city")));
            bean.setProvinceCode(cursor.getString(cursor.getColumnIndex("provinceCode")));
            bean.setProvince(cursor.getString(cursor.getColumnIndex("province")));
            resultList.add(bean);
        }
        db.close();
        return resultList;
    }

    /**
     * 通过areaCode获取一条历史记录
     *
     * @param areaCode
     * @return
     */
    private CityHistoryBean getHistoryDataByAreaCode(String areaCode) {
        CityHistoryBean bean = null;
        SQLiteDatabase db = openHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from city_history where areaCode=?", new String[]{areaCode});
        while (cursor.moveToNext()) {
            bean = new CityHistoryBean();
            bean.setAreaCode(cursor.getString(cursor.getColumnIndex("areaCode")));
            bean.setArea(cursor.getString(cursor.getColumnIndex("area")));
            bean.setCityCode(cursor.getString(cursor.getColumnIndex("cityCode")));
            bean.setCity(cursor.getString(cursor.getColumnIndex("city")));
            bean.setProvinceCode(cursor.getString(cursor.getColumnIndex("provinceCode")));
            bean.setProvince(cursor.getString(cursor.getColumnIndex("province")));
            break;
        }
        db.close();
        return bean;
    }

    /**
     * 通过areaCode更新一条历史记录
     *
     * @param bean
     * @return
     */
    private int updateHistoryData(CityHistoryBean bean) {
        ContentValues cv = new ContentValues();
        cv.put("areaCode", bean.getAreaCode());
        cv.put("area", bean.getArea());
        cv.put("cityCode", bean.getCityCode());
        cv.put("city", bean.getCity());
        cv.put("provinceCode", bean.getProvinceCode());
        cv.put("province", bean.getProvince());
        cv.put("time", System.currentTimeMillis());
        SQLiteDatabase db = openHelper.getWritableDatabase();
        int result = db.update(tableName, cv, "areaCode=?", new String[]{bean.getAreaCode()});
        db.close();
        return result;
    }
}


